package com.itheima.service.impl;

import com.github.pagehelper.PageHelper;
import com.itheima.dao.UserDao;
import com.itheima.domain.User;
import com.itheima.service.UserService;
import com.itheima.utils.SqlSessionUtils;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

public class UserServiceImpl implements UserService {

    @Override
    public List<User> findAll() {
        SqlSession sqlSession = SqlSessionUtils.getSqlSession();
        UserDao userDao = sqlSession.getMapper(UserDao.class);

//        // 分页设置
//        PageHelper.startPage(1,15);

        return userDao.findAll();
    }

    @Override
    public void addUser(User user) {
        SqlSession sqlSession = SqlSessionUtils.getSqlSession();
        UserDao userDao = sqlSession.getMapper(UserDao.class);
        userDao.addUser(user);
        sqlSession.commit();
        sqlSession.close();
    }

    @Override
    public void deleteById(String id) {
        SqlSession sqlSession = SqlSessionUtils.getSqlSession();
        UserDao userDao = sqlSession.getMapper(UserDao.class);
        userDao.deleteById(id);
        sqlSession.commit();
        sqlSession.close();
    }

    @Override
    public User findById(String id) {
        SqlSession sqlSession = SqlSessionUtils.getSqlSession();
        UserDao userDao = sqlSession.getMapper(UserDao.class);
        return userDao.findById(id);
    }

    @Override
    public void updateUser(User user) {
        SqlSession sqlSession = SqlSessionUtils.getSqlSession();
        UserDao userDao = sqlSession.getMapper(UserDao.class);
        userDao.updateUser(user);
        sqlSession.commit();
        sqlSession.close();
    }
}
